<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('ROOT')) die();
if (isset($_POST['mass'])) {
	$in = implode(',', $_POST['slave']);
	switch ($_POST['mass']) {
		case '1':
			Mysql::getInstance()->query("UPDATE `" . PREFIX . "_news` SET is_moderated=0 WHERE id IN($in)");
			break;
		case '2':
			Mysql::getInstance()->query("UPDATE `" . PREFIX . "_news` SET is_moderated=1 WHERE id IN($in)");
			break;
		case '3':
			Mysql::getInstance()->query("UPDATE `" . PREFIX . "_news` SET time_add='" . TIME . "' WHERE id IN($in)");
			break;
		case '4':
			Mysql::getInstance()->query("DELETE FROM `" . PREFIX . "_news` WHERE id IN($in)");
			break;
		case '5':
			Mysql::getInstance()->query("UPDATE `" . PREFIX . "_news` SET is_fixed='1' WHERE id IN($in)");
			break;
		case '6':
			Mysql::getInstance()->query("UPDATE `" . PREFIX . "_news` SET is_fixed='0' WHERE id IN($in)");
			break;
	}
	Aurus::getInstance()->addContent( Adminarea::getInstance()->adminInfo(__('Зміни збережено'), 1, '', 3));
} else {
    if(!MASS){
        $sel = ' WHERE is_moderated=\'0\'';
    } else {
        $sel = '';
    }

	$pp = 30;
	$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
	$pst = ($page - 1) * $pp;
	$news = Mysql::getInstance()->query("SELECT SQL_CALC_FOUND_ROWS id,title,category,is_fixed,login,is_moderated FROM `" . PREFIX . "_news` LEFT JOIN " . PREFIX . "_users ON " . PREFIX . "_users.uid=" . PREFIX . "_news.author $sel ORDER BY is_fixed DESC,time_add ".Config::get('newsOrderBy','news')." LIMIT $pst,$pp");
	$count = Mysql::getInstance()->getarray("SELECT FOUND_ROWS() as count");
	$count = $count['count'];
	if (!$count) {
		Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Новин немає'), 1));
	} else {
		$nav = Modules::getInstance()->genNavAsText($page, $pp, $count, LINK_ROOT . 'adminarea/?do=news&amp;todo=mass&amp;page=') . '<br />';
		$c = array();
		$r = Html::tr(Html::td(__('Назва новини'), 'chead', 'left', '15%') . Html::td(__('Категорія'), 'chead', 'left', '15%') . Html::td(__('Новина зафіксована?'), 'chead', 'left', '15%') . Html::td(__('Автор'), 'chead', 'left', '15%') . Html::td(__('Новина промодерована?'), 'chead', 'left', '15%') . Html::td('<input type="checkbox" id="master" onclick="markAll(this);">', 'chead', 'left', '15%'));
		while ($each = Mysql::getInstance()->fetch_assoc($news)) {
			$d = Html::td(stripslashes($each['title'] . ' <a href="' . LINK_ROOT . 'adminarea/?do=news&amp;todo=edit&amp;eid=' . $each['id'] . '">[' . __('Змінити') . ']</a>'), 'confclass', 'left', '30%');
			$d .= Html::td(Factory::get('Module_news')->getCatById($each['category']), 'confclass', 'left', '25%');
			$d .= Html::td(boolean($each['is_fixed']) ? __('Зафіксовано') : __('Незафіксовано'), 'confclass', 'left', '15%');
			$d .= Html::td($each['login'], 'confclass', 'left', '16%');
			$d .= Html::td(boolean($each['is_moderated']) ? __('Промодеровано') : __('Не промодеровано'), 'confclass', 'left', '7%');
			$d .= Html::td('<input type="checkbox" name="slave[]" value="' . $each['id'] . '">', 'confclass', 'left', '7%');
			$r .= Html::tr($d);
		}
		$scr = '<script type="text/javascript">function markAll(there){var cboxes = document.getElementsByName("slave[]");for(i = 0;i < cboxes.length;i++){if(there.checked){cboxes[i].checked = true;} else {cboxes[i].checked = false;}}}</script>';
		$mlist = '<select name="mass">';
		$mlist .= '<option value="0">' . __('Виберіть дію') . '</option>';
		$mlist .= '<option value="1">' . __('Зняти з публікації') . '</option>';
		$mlist .= '<option value="2">' . __('Опублікувати') . '</option>';
		$mlist .= '<option value="3">' . __('Встановити поточну дату') . '</option>';
		$mlist .= '<option value="4">' . __('Видалити') . '</option>';
		$mlist .= '<option value="5">' . __('Зафіксувати') . '</option>';
		$mlist .= '<option value="6">' . __('Зняти фіксацію') . '</option>';
		$mlist .= '</select>';
		$mlist .= '<input type="submit" value="' . __('Вперед') . '" />';

		Aurus::getInstance()->addContent( $scr . $nav . Aurus::getInstance()->form_start() . Html::table($r) . $mlist . '</form>' . $nav);
	}
}